package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Upload;
import com.cloudwise.gproject.service.CommonFileService;
import com.cloudwise.gproject.service.UploadService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * (Upload)表控制层
 *
 * @author makejava
 * @since 2021-03-24 10:40:10
 */
@Controller
@RequestMapping("upload")
public class UploadController {
    /**
     * 服务对象
     */
    @Resource
    private UploadService uploadService;
    @Resource
    private CommonFileService commonFileService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.uploadService.queryById(id));
    }

    /**
     * 跳转部门分页列表页面
     *
     * @return
     */
    @RequestMapping("toPage")
    public String toPage() {
        return "back/upload-page";
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Integer page, Integer limit, Upload upload) {
        PageInfo pageInfo = uploadService.queryAll(page, limit, upload);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     *
     * @param upload 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Upload upload) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.uploadService.insert(upload));
    }

    /**
     * 修改数据
     *
     * @param upload 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Upload upload) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.uploadService.update(upload));
    }

    /**
     * 通过主键删除数据
     *
     * @param uploadId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer uploadId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.uploadService.deleteById(uploadId));
    }
    @ResponseBody
    @RequestMapping("updateFile")
    public Result updateFile(@RequestParam MultipartFile file) {
        Map map = commonFileService.uploadFile(file, "files/upload");
        System.out.println(map.get("fileName"));
        System.out.println(map.get("filePath"));
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                map);
    }

    @ResponseBody
    @RequestMapping("addFile")
    public Result addFile(@RequestParam MultipartFile file) {
        Map map = commonFileService.uploadFile(file, "files/upload");
        System.out.println(map.get("fileName"));
        System.out.println(map.get("filePath"));
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                map);
    }
}
